package com.htfyun.factorytest.engineerui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;

import androidx.core.app.ActivityCompat;
import com.htfyun.factorytest.R;
import com.htfyun.factorytest.ViewDialogFragment;


/**
 * Created by sst on 2018/4/23.
 */

public class VcomActivity extends Activity {
    private static final String TAG = VcomActivity.class.getSimpleName();

    //echo 10,1460 > /sys/class/sm8951/dbg
    private static final String WRITE_VCOM_PATH = "/sys/class/sm8951/dbg";

    private TextView txtPasswd1;
    private TextView txtPasswd2;
    private TextView txtPasswd3;
    private TextView txtPasswd4;
    private List<TextView> txtPasswdList = new ArrayList<>();

    private TextView txtSettingsPasswd1;
    private TextView txtSettingsPasswd2;
    private TextView txtSettingsPasswd3;
    private TextView txtSettingsPasswd4;
    private TextView txtSettingsPasswd5;
    private TextView txtSettingsPasswd6;
    private TextView txtSettingsPasswd7;
    private TextView txtSettingsPasswd8;
    private TextView txtSettingsPasswd9;
    private TextView txtSettingsPasswd0;
    private TextView txtSettingsPasswdDot;
    private TextView txtSettingsPasswdDelete;

    private Button btnVcom;
    private Button btnFinish;

    private String mChosenPassword = "";
    private int writeVcomValue = -1;

    private int result = RESULT_CANCELED;

    private TextView txtCurrentVcom;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        setContentView(R.layout.activity_vcom);

        txtCurrentVcom = (TextView) findViewById(R.id.txtCurrentVcom);

        //txtCurrentVcom.setText(getString(R.string.vcom_current_value, SPIFlashExtraData.getInstance().getVcom() * 1.0 / 1000));

        txtPasswd1 = (TextView) findViewById(R.id.txtPasswd1);
        txtPasswd2 = (TextView) findViewById(R.id.txtPasswd2);
        txtPasswd3 = (TextView) findViewById(R.id.txtPasswd3);
        txtPasswd4 = (TextView) findViewById(R.id.txtPasswd4);
        txtPasswdList.add(txtPasswd1);
        txtPasswdList.add(txtPasswd2);
        txtPasswdList.add(txtPasswd3);
        txtPasswdList.add(txtPasswd4);

        txtSettingsPasswd1 = (TextView) findViewById(R.id.txtSettingsPasswd1);
        txtSettingsPasswd2 = (TextView) findViewById(R.id.txtSettingsPasswd2);
        txtSettingsPasswd3 = (TextView) findViewById(R.id.txtSettingsPasswd3);
        txtSettingsPasswd4 = (TextView) findViewById(R.id.txtSettingsPasswd4);
        txtSettingsPasswd5 = (TextView) findViewById(R.id.txtSettingsPasswd5);
        txtSettingsPasswd6 = (TextView) findViewById(R.id.txtSettingsPasswd6);
        txtSettingsPasswd7 = (TextView) findViewById(R.id.txtSettingsPasswd7);
        txtSettingsPasswd8 = (TextView) findViewById(R.id.txtSettingsPasswd8);
        txtSettingsPasswd9 = (TextView) findViewById(R.id.txtSettingsPasswd9);
        txtSettingsPasswd0 = (TextView) findViewById(R.id.txtSettingsPasswd0);
        txtSettingsPasswdDot = (TextView) findViewById(R.id.txtSettingsPasswdDot);
        txtSettingsPasswdDelete = (TextView) findViewById(R.id.txtSettingsPasswdDelete);

        btnVcom = (Button) findViewById(R.id.btnVcom);
        btnVcom.setElevation(0.0f);
        btnFinish = (Button) findViewById(R.id.btnFinish);

        txtSettingsPasswd1.setOnClickListener(onClickListener);
        txtSettingsPasswd2.setOnClickListener(onClickListener);
        txtSettingsPasswd3.setOnClickListener(onClickListener);
        txtSettingsPasswd4.setOnClickListener(onClickListener);
        txtSettingsPasswd5.setOnClickListener(onClickListener);
        txtSettingsPasswd6.setOnClickListener(onClickListener);
        txtSettingsPasswd7.setOnClickListener(onClickListener);
        txtSettingsPasswd8.setOnClickListener(onClickListener);
        txtSettingsPasswd9.setOnClickListener(onClickListener);
        txtSettingsPasswd0.setOnClickListener(onClickListener);
        txtSettingsPasswdDot.setOnClickListener(onClickListener);
        txtSettingsPasswdDelete.setOnClickListener(onClickListener);

        btnVcom.setEnabled(false);
        btnVcom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e(TAG, "vcom = " + mChosenPassword );

                float value = Float.valueOf(mChosenPassword);
                if (Math.abs(value) > 5) {

                    showFinishDialog(R.string.vcom_write_err_title, R.string.vcom_write_err_msg);

                } else {

                    writeVcomPath((value));

                    txtCurrentVcom.setText(getString(R.string.vcom_current_value, writeVcomValue * 1.0 / 1000));

                }

            }
        });

        btnFinish.setEnabled(false);
        btnFinish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //writeResultIntoDB(getResources().getString(R.string.test_ok), writeVcomValue);

                Intent intent = new Intent();
                intent.putExtra("writeVcomValue", writeVcomValue);
                setResult(result, intent);

                ActivityCompat.finishAfterTransition(VcomActivity.this);

            }
        });
    }

    private void showFinishDialog(int titleRes, int msgRes) {
        final String title = getString(titleRes);
        final String msg = getString(msgRes);
        ViewDialogFragment dialog = ViewDialogFragment.newInstance(title, msg);

        dialog.show(getFragmentManager(),
                new ViewDialogFragment.Callback() {
                    @Override
                    public void onClick() {

                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                mChosenPassword = "";
                                updateTxtPasswd();
                            }
                        });

                    }
                }, null);
    }

    private void writeVcomPath(float value) {
        int writeValue = (int) (value * 1000);
        writeVcomValue = writeValue;
        result = RESULT_OK;
        btnFinish.setEnabled(true);

        try {
            FileWriter fw = new FileWriter(WRITE_VCOM_PATH, false);
            BufferedWriter bw = new BufferedWriter(fw);
            Log.e(TAG, "10," + writeValue);
            bw.write("10," + writeValue);// 往已有的文件上添加字符串
            bw.flush();
            fw.flush();
            bw.close();
            fw.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private void updateTxtPasswd() {

        for (int i = 0; i < 4; i++) {
            String txt = "";
            if (i < mChosenPassword.length()) {
                txt = "" + mChosenPassword.charAt(i);
            }
            txtPasswdList.get(i).setText(txt);
        }


        btnVcom.setEnabled((mChosenPassword.length() == 4) );
    }


    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (!(v instanceof TextView)) {
                return;
            }

            TextView txt = (TextView) v;

            if (v == txtSettingsPasswdDelete) {

                if (!TextUtils.isEmpty(mChosenPassword)) {
                    mChosenPassword = mChosenPassword.substring(0, mChosenPassword.length() - 1);

                }

                Log.e(TAG, " mChosenPassword = " + mChosenPassword);

            } else {

                if (mChosenPassword.length() < 4) {
                    StringBuilder sb = new StringBuilder(mChosenPassword);
                    sb.append(txt.getText());
                    mChosenPassword = sb.toString();
                    Log.e(TAG, "sb.length = " + sb.length() + ", mChosenPassword = " + mChosenPassword);

                }
            }

            updateTxtPasswd();

        }
    };

}
